
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 04/29/2012 22:16:00
-- Generated from EDMX file: C:\Users\MMR\Documents\Visual Studio 2010\Projects\SydEnergiDataService\SydEnergiDataService\SydEnergiDataServiceDb.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [SydEnergiDataServiceDb];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_CabinetDtoAddressDto]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CabinetDtos] DROP CONSTRAINT [FK_CabinetDtoAddressDto];
GO
IF OBJECT_ID(N'[dbo].[FK_EntryDtoAddressDto]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EntryDtos] DROP CONSTRAINT [FK_EntryDtoAddressDto];
GO
IF OBJECT_ID(N'[dbo].[FK_EntryDtoHistoryEntryDto]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[HistoryEntryDtoSet] DROP CONSTRAINT [FK_EntryDtoHistoryEntryDto];
GO
IF OBJECT_ID(N'[dbo].[FK_CabinetDtoEntryDto]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[EntryDtos] DROP CONSTRAINT [FK_CabinetDtoEntryDto];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[AddressDtos]', 'U') IS NOT NULL
    DROP TABLE [dbo].[AddressDtos];
GO
IF OBJECT_ID(N'[dbo].[CabinetDtos]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CabinetDtos];
GO
IF OBJECT_ID(N'[dbo].[EntryDtos]', 'U') IS NOT NULL
    DROP TABLE [dbo].[EntryDtos];
GO
IF OBJECT_ID(N'[dbo].[HistoryEntryDtoSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[HistoryEntryDtoSet];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'AddressDtos'
CREATE TABLE [dbo].[AddressDtos] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [StreetName] nvarchar(max)  NOT NULL,
    [StreetNumber] int  NOT NULL,
    [ZipCode] int  NOT NULL,
    [City] nvarchar(max)  NOT NULL,
    [Letter] nvarchar(max)  NOT NULL,
    [Floor] nvarchar(max)  NOT NULL,
    [StairCasePosition] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'CabinetDtos'
CREATE TABLE [dbo].[CabinetDtos] (
    [CabinetId] nvarchar(max)  NOT NULL,
    [Latitude] float  NOT NULL,
    [Longitude] float  NOT NULL,
    [AddressDto_Id] int  NOT NULL
);
GO

-- Creating table 'EntryDtos'
CREATE TABLE [dbo].[EntryDtos] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Fiber] nvarchar(max)  NOT NULL,
    [Tube] nvarchar(max)  NOT NULL,
    [Cassette] int  NOT NULL,
    [Position] int  NOT NULL,
    [Teletube] nvarchar(max)  NOT NULL,
    [InnerTubeId] nvarchar(max)  NOT NULL,
    [InnerTube] int  NOT NULL,
    [LsseMb] nvarchar(max)  NOT NULL,
    [Cable] nvarchar(max)  NOT NULL,
    [FiberNumber] int  NOT NULL,
    [CabinetId] nvarchar(max)  NULL,
    [AddressDto_Id] int  NOT NULL
);
GO

-- Creating table 'HistoryEntryDtoSet'
CREATE TABLE [dbo].[HistoryEntryDtoSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Date] datetime  NOT NULL,
    [User] nvarchar(max)  NOT NULL,
    [Reason] nvarchar(max)  NOT NULL,
    [Comment] nvarchar(max)  NOT NULL,
    [EntryDtoId] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'AddressDtos'
ALTER TABLE [dbo].[AddressDtos]
ADD CONSTRAINT [PK_AddressDtos]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [CabinetId] in table 'CabinetDtos'
ALTER TABLE [dbo].[CabinetDtos]
ADD CONSTRAINT [PK_CabinetDtos]
    PRIMARY KEY CLUSTERED ([CabinetId] ASC);
GO

-- Creating primary key on [Id] in table 'EntryDtos'
ALTER TABLE [dbo].[EntryDtos]
ADD CONSTRAINT [PK_EntryDtos]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'HistoryEntryDtoSet'
ALTER TABLE [dbo].[HistoryEntryDtoSet]
ADD CONSTRAINT [PK_HistoryEntryDtoSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [AddressDto_Id] in table 'CabinetDtos'
ALTER TABLE [dbo].[CabinetDtos]
ADD CONSTRAINT [FK_CabinetDtoAddressDto]
    FOREIGN KEY ([AddressDto_Id])
    REFERENCES [dbo].[AddressDtos]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CabinetDtoAddressDto'
CREATE INDEX [IX_FK_CabinetDtoAddressDto]
ON [dbo].[CabinetDtos]
    ([AddressDto_Id]);
GO

-- Creating foreign key on [AddressDto_Id] in table 'EntryDtos'
ALTER TABLE [dbo].[EntryDtos]
ADD CONSTRAINT [FK_EntryDtoAddressDto]
    FOREIGN KEY ([AddressDto_Id])
    REFERENCES [dbo].[AddressDtos]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_EntryDtoAddressDto'
CREATE INDEX [IX_FK_EntryDtoAddressDto]
ON [dbo].[EntryDtos]
    ([AddressDto_Id]);
GO

-- Creating foreign key on [EntryDtoId] in table 'HistoryEntryDtoSet'
ALTER TABLE [dbo].[HistoryEntryDtoSet]
ADD CONSTRAINT [FK_EntryDtoHistoryEntryDto]
    FOREIGN KEY ([EntryDtoId])
    REFERENCES [dbo].[EntryDtos]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_EntryDtoHistoryEntryDto'
CREATE INDEX [IX_FK_EntryDtoHistoryEntryDto]
ON [dbo].[HistoryEntryDtoSet]
    ([EntryDtoId]);
GO

-- Creating foreign key on [CabinetId] in table 'EntryDtos'
ALTER TABLE [dbo].[EntryDtos]
ADD CONSTRAINT [FK_CabinetDtoEntryDto]
    FOREIGN KEY ([CabinetId])
    REFERENCES [dbo].[CabinetDtos]
        ([CabinetId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CabinetDtoEntryDto'
CREATE INDEX [IX_FK_CabinetDtoEntryDto]
ON [dbo].[EntryDtos]
    ([CabinetId]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------